package org.kolyas.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import org.kolyas.domain.GoodBean;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JTextPane;
import javax.swing.JEditorPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;
import java.awt.Color;

public class MainWindow {

	private JFrame frame;
	private JTextField textFieldCode;
	private JTextField textFieldName;
	private ArrayList<GoodBean> list;
	private JButton btnList;
	private JLabel lblPrice;
	private JTextField textFieldPrice;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		list = new ArrayList<GoodBean>();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 202);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblCode = new JLabel("Code");
		lblCode.setBounds(10, 11, 46, 14);
		frame.getContentPane().add(lblCode);
		
		setTextField(new JTextField());
		getTextField().setBounds(58, 8, 86, 20);
		frame.getContentPane().add(getTextField());
		getTextField().setColumns(10);
		
		JButton btnNewButton = new JButton("New button");
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GoodBean shozavgodno = new GoodBean();
				shozavgodno.code = Integer.parseInt(textFieldCode.getText());
				shozavgodno.price = Integer.parseInt(textFieldPrice.getText());
				shozavgodno.name = textFieldName.getText();
				list.add(shozavgodno);
			}
		});
		
		btnNewButton.setBounds(58, 98, 89, 23);
		frame.getContentPane().add(btnNewButton);
		
		textFieldName = new JTextField();
		textFieldName.setText("2");
		textFieldName.setBounds(58, 39, 86, 20);
		frame.getContentPane().add(textFieldName);
		textFieldName.setColumns(10);
		
		btnList = new JButton("List");
		btnList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for (GoodBean shozavgodno2 : list){
					System.out.println(shozavgodno2.code +" : "+shozavgodno2.name+" : "+shozavgodno2.price);
					((AbstractTableModel) table.getModel()).fireTableDataChanged();
				}
			}
		});
		btnList.setBounds(58, 132, 89, 23);
		frame.getContentPane().add(btnList);
		
		lblPrice = new JLabel("Price");
		lblPrice.setBounds(10, 67, 46, 14);
		frame.getContentPane().add(lblPrice);
		
		textFieldPrice = new JTextField();
		textFieldPrice.setText("3");
		textFieldPrice.setBounds(58, 67, 86, 20);
		frame.getContentPane().add(textFieldPrice);
		textFieldPrice.setColumns(10);
		
		table = new JTable();
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		table.setBounds(157, 11, 240, 147);
		frame.getContentPane().add(table);
		//		table.setColumnModel(new TableColumnModel());
				table.setModel(new DefaultTableModel(
					new Object[][] {
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
					},
					new String[] {
						"Code", "Name", "Price"
					}
				) {
					Class[] columnTypes = new Class[] {
						Integer.class, String.class, Integer.class
					};
					public Class getColumnClass(int columnIndex) {
						return columnTypes[columnIndex];
					}
				});
				table.getColumnModel().getColumn(0).setPreferredWidth(15);
				table.getColumnModel().getColumn(0).setMaxWidth(80);
				table.getColumnModel().getColumn(1).setPreferredWidth(50);
				table.getColumnModel().getColumn(1).setMaxWidth(80);
				table.getColumnModel().getColumn(2).setPreferredWidth(15);
				table.getColumnModel().getColumn(2).setMaxWidth(800);
	}
	

	public JTextField getTextField() {
		return textFieldCode;
	}

	public void setTextField(JTextField textField) {
		this.textFieldCode = textField;
		textFieldCode.setText("1");
	}
}
